#include "PhysicsObject.h"


void PhysicsObject::Initialise(int xPos, int yPos, int w, int h, float m, float restitution, bool Hit)
{
	x = xPos;
	y = yPos;
	width = w;
	height = h;
	mass = m;
	e = restitution;
	velocity = 0;
	time = 0;
	hit = Hit;
	isLaunched = false;
	theta = 0;
	for (int i = 0; i < 10; i++)
	{
		hitting[i] = false;
	}
}

bool PhysicsObject::isColliding(PhysicsObject* object)
{
	if (x + width < object->getX())			{return false;}
	if (x > object->getX() + object->getWidth())	{return false;}
	if (y + height < object->getY())			{return false;}
	if (y > object->getY() + object->getHeight()) {return false;}
	return true;
}

void PhysicsObject::Launch()
{
        float gradient = -1 * (float)(ProjectileOriginalY - y) / (float)(ProjectileOriginalX - x);
        theta = atanf(gradient) / (PI / 180);
        float distance = sqrtf(((ProjectileOriginalX - x) * (ProjectileOriginalX - x)) + ((ProjectileOriginalY - y) * (ProjectileOriginalY - y)));
        velocity = 450 * (distance / 150);
        time = 0;
        originalX = x;
        originalY = y;
        isLaunched = true;
}

void PhysicsObject::ProjectileUpdate(float eTime)
{
	float xPos, yPos = 0;
	if (hit)
	{
		time += eTime / 1000;
		xPos = velocity * time * cos(theta * PI/180);
		yPos = (velocity * time * sin(theta * PI / 180)) - (0.5 * gravity * (time * time));
		x = originalX + xPos;
		y = originalY - yPos;
	}
	//velocity = (gravity * time) / (sin(theta * PI / 180));
	//theta = e * theta;

}

void PhysicsObject::ToggleLaunched()
{
	if (isLaunched == true)
	{
		isLaunched = false;
	}
	else
	{
		isLaunched = true;
	}
}

void PhysicsObject::Fire()
{
	time = 0;
	isLaunched = true;
	originalX = x;
	originalY = y;
}